using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Verben
{
	public class Verb
	{
		private string mInfinitiv;
		public string Infinitiv
		{
			get
			{
				return mInfinitiv;
			}
			set
			{
				mInfinitiv = value;
			}
		}

		public string IsTrennbar()
		{
			string[] prefixe = File.ReadAllLines("TrennbarPrefix.txt");
			foreach (string prefix in prefixe)
			{
				if (Infinitiv.StartsWith(prefix))
				{
					return prefix;
				}
			}
			return null;
		}

		public bool IsWordInFile(string word, string fileName)
		{
			string[] allWords = File.ReadAllLines(fileName);
			foreach (string line in allWords)
			{
				if (line == word)
				{
					return true;
				}
			}
			return false;
		}

		public void CopyFrom(Verb v)
		{
			Present.CopyFrom(v.Present);
			Past.CopyFrom(v.Past);
		}

		public virtual void Fill()
		{
			if (Infinitiv.Length < 2)
			{
				return;
			}

			Verb irregExistiert = XmlZeug.Irregular.Find(Infinitiv);
			if (irregExistiert != null)
			{
				CopyFrom(irregExistiert);
				return;
			}

			string anfang;
			if (Infinitiv.EndsWith("en"))
			{
				anfang = Infinitiv.Substring(0, Infinitiv.Length - 2);
			}
			else if(Infinitiv.EndsWith("eln"))
			{
				anfang = Infinitiv.Substring(0, Infinitiv.Length - 3);
			}
			else
			{
				anfang = Infinitiv;
			}
			
			string prefix = IsTrennbar();

			if (prefix != null && anfang.Length > prefix.Length)
			{
				anfang = anfang.Substring(prefix.Length);
			}

			if (Infinitiv.EndsWith("en"))
			{
				Present.Ich = anfang + "e";
				if (anfang.EndsWith("t"))
				{
					Present.Du = anfang + "est";
					Present.Er = anfang + "et";
					Present.Ihr = anfang + "et";
				}
				else
				{
					Present.Du = anfang + "st";
					Present.Er = anfang + "t";
					Present.Ihr = anfang + "t";
				}
				Present.Wir = anfang + "en";
				Present.Sie = anfang + "en";
			
				Past.Ich = anfang + "te";
				Past.Du = anfang + "test";
				Past.Er = anfang + "te";
				Past.Wir = anfang + "ten";
				Past.Ihr = anfang + "tet";
				Past.Sie = anfang + "ten";
			}
			else if (Infinitiv.EndsWith("eln"))
			{
				Present.Ich = anfang + "le";
				Present.Du = anfang + "elst";
				Present.Er = anfang + "elt";
				Present.Wir = anfang + "eln";
				Present.Ihr = anfang + "elt";
				Present.Sie = anfang + "eln";
				Past.Ich = anfang + "elte";
				Past.Du = anfang + "eltest";
				Past.Er = anfang + "elte";
				Past.Wir = anfang + "elten";
				Past.Ihr = anfang + "eltet";
				Past.Sie = anfang + "elten";
			}

			if (prefix != null)
			{
				Present.AppendSuffix(" " + prefix);
				Past.AppendSuffix(" " + prefix);
			}
		}

		public VerbForm Present = new VerbForm();
		public VerbForm Past = new VerbForm();
	}
}
